package com.ykdl.member.kid.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyCouponAdapter;
import com.ykdl.member.kid.adapters.MyCouponOnLineAdapter;
import com.ykdl.member.kid.adapters.OnlineMyCouponAdapter;
import com.ykdl.member.kid.beans.OnLineCouponBean;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.beans.TiketOnLineBean;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.member.kid.xlist.XListView;
import com.ykdl.member.kid.xlist.XListView.IXListViewListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

/********************************************************************************
 * 
 ** 作者： jiafeiyu 创始时间：2015-7-24 描述： 我的优惠券之已过期--在线支付可用列表
 *********************************************************************************/
public class MyCouponOnLineOutOfDateFragment extends BaseFragment implements
		IXListViewListener {
	private View convertView;

	private XListView mRefListView;
	private OnlineMyCouponAdapter adapter;
	private List<OnLineCouponBean> tickets;
	private int cursor = 0;
	private int count = 20;
	private	boolean isGoneBt=false;
	@Override
	protected void onNewCreateView(View view) {
		convertView = view;
		setCenterView(R.layout.fragment_coupon);
		initView();
	}

	private void initView() {
		tickets = new ArrayList<OnLineCouponBean>();
		adapter = new OnlineMyCouponAdapter(mContext, 3);
		mRefListView = (XListView) convertView.findViewById(R.id.mycoupon_list);
		mRefListView.setPullLoadEnable(true);
		// 允许下拉
		mRefListView.setPullRefreshEnable(true);
		// 设置监听器
		mRefListView.setXListViewListener(this);
		mRefListView.gonefoot(false);
		mRefListView.hideFoot();
		mRefListView.setAdapter(adapter);
		mRefListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}
		});
		setIsShowLoad(true);
		getData();
	}

	// 获取网络数据
	public void getData() {
		String url = KidConfig.MY_COUPON_LIST_EXPIRED;
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, map, null);
		TaskManager.startHttpRequest(request, new getDataTag(),
				TiketOnLineBean.class);
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			setIsShowLoad(false);
			if (t instanceof TiketOnLineBean) {
				TiketOnLineBean bean = (TiketOnLineBean) t;
				if (bean.getError() == null) {
					if (bean.getTickets() == null || bean.getTickets().size() == 0) {
						showErrorView(true, "暂无此类优惠券");
					} else {
						tickets.addAll(bean.getTickets());
						
						if (bean.getNext_cursor() == bean.getTotal_number()) {
							mRefListView.endLoadMore();
							isGoneBt = true;
						} else {
							mRefListView.readyLoadMore();
							cursor = bean.getNext_cursor();
						}
						adapter.setmOnLineList(tickets);
						adapter.notifyDataSetChanged();
						mRefListView.stopRefresh();
						mRefListView.showFoot();
					}
				}
			}

		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			// Toast.makeText(mContext, "加载失败", 1).show();
			setIsShowLoad(false);
			showErrorView(true, "暂无此类优惠券");
			mRefListView.stopRefresh();
			mRefListView.endLoadMore();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub

		}

	}

	@Override
	protected void tryAgain() {
		getData();
	}

	@Override
	public void onRefresh() {
		tickets.clear();
		cursor=0;
		isGoneBt  = false;
		getData();
	}

	@Override
	public void onLoadMore() {
		
		if (isGoneBt == false) {
			
			getData();
		} else {
			mRefListView.endLoadMore();
			mRefListView.stopRefresh();
		}

	}

}
